import grpc
from protos import user_pb2,user_pb2_grpc,address_pb2,address_pb2_grpc

def test_create_user(stub):
        request=user_pb2.CreateUserRequest() # type: ignore
        request.mobile='13800000003'
        response = stub.CreateUser(request)
        print(response)

def test_get_user_by_id(stub):
    request=user_pb2.IdRequest()# type: ignore
    request.id=1955620451921166336
    response=stub.GetUserById(request)
    print(response)
    
def test_get_user_by_mobile(stub):
    request=user_pb2.MobileRequest()# type: ignore
    request.mobile='13800000003'
    response=stub.GetUserByMobile(request)
    print(response)
    
def test_update_avatar(stub):
    request=user_pb2.AvatarRequest()# type: ignore
    request.id=1955620451921166336
    request.avatar='https://p3-search.byteimg.com/obj/labis/4deb5daeca906e391867bf7c562d12b1'
    response=stub.UpdateAvatar(request)
    print(response)
    
def test_update_username(stub):
    request=user_pb2.UsernameRequest()# type: ignore
    request.id=1955620451921166336
    request.username='wch'
    response=stub.UpdateUsername(request)
    print(response)
    
def test_update_password(stub):
    request=user_pb2.PasswordRequest()# type: ignore
    request.id=1955620451921166336
    request.password='123456'
    response=stub.UpdatePassword(request)
    print(response)
    
def test_GetUserList(stub):
    request=user_pb2.PageRequest()# type: ignore
    request.page=1
    request.page_size=10
    response=stub.GetUserList(request)
    print(response.user_list)
    print(response.total)
    
def test_VerifyUser(stub):
    request=user_pb2.VerifyUserRequest()# type: ignore
    request.mobile='13800000000'
    request.password='123456'
    response=stub.VerifyUser(request)
    print(response)

def test_GetOrCreateUserByMobile(stub):
    request=user_pb2.MobileRequest()# type: ignore
    request.mobile='18019475322'
    response=stub.GetOrCreateUserByMobile(request)
    print(response)
    
def test_create_address(stub):
    request = address_pb2.CreateAddressRequest(# type: ignore
        user_id=1955620451921166336,
        realname='孙悟空',
        mobile='18899991111',
        region='北京市朝阳区',
        detail='白家庄东里',
    )
    response = stub.CreateAddress(request)
    print(response.address)
    
def test_get_address_list(stub):
    request = address_pb2.AddressListRequest(# type: ignore
        user_id=1956220698842103808,
        page=1,
        size=10,
    )
    response = stub.GetAddressList(request)
    print(response)
    
def test_get_address_detail(stub):
    request = address_pb2.AddressIdRequest(# type: ignore
        id='65e2be962ea148169a2e30582a07847c',
        user_id=1956220698842103808,
    )
    response = stub.GetAddressById(request)
    print(response)

def test_update_address(stub):
    request = address_pb2.UpdateAddressRequest(# type: ignore
        id='65e2be962ea148169a2e30582a07847c',
        user_id=1956220698842103808,
        realname='戈萍',
        mobile='16313093025',
        region='西北',
        detail='魔兽山庄',
    )
    response = stub.UpdateAddress(request)
    print(response)

def main():
    with grpc.insecure_channel('127.0.0.1:48059') as channel:
        # stub = user_pb2_grpc.UserStub(channel)
        address_stub = address_pb2_grpc.AddressStub(channel)
        user_stub: user_pb2_grpc.UserStub = user_pb2_grpc.UserStub(channel)
        try:
            # test_get_address_list(address_stub)
            test_GetOrCreateUserByMobile(user_stub)
        except grpc.RpcError as e:
            print(e.code())
            print(e.details())
        
    
        
if __name__ == '__main__':
    main()

